﻿//-----------------------------------------------------------------------
// <copyright file="Program.cs" company="DylanBox">
// Copyright (c) DylanBox Corporation. All rights reserved.
// </copyright>
// <summary></summary>
// <reference>
//      <<1>>
//    This is a very basic and simple demo using delegates - primarily callback
//      ref: http://www.dotnetscraps.com/dotnetscraps/post/Explaining-Delegates-in-C.aspx
// </reference>
// <author>Dylan</author>
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CallBackDemo
{
    public class Example2
    {
        // Declaration - Take 1 parameter, return nothing
        public delegate void LogHandler(string message);

        // Instantiation - Create a function which takes delegate as one parameter
        // Verify if it is null before you use it
        public void Process(LogHandler logHandler)
        {
            if (logHandler != null)
            {
                logHandler("Process() begin");
            }

            if (logHandler != null)
            {
                logHandler("Process() end");
            }
        }
    }    

    class Program
    {
        // Create a method with the same signature as the delegate
        static void Logger(string s)
        {
            Console.WriteLine(s);
        }

        public static void Main(string[] args)
        {   
            // Invocation in the client
            Example2.LogHandler myLogger = new Example2.LogHandler(Logger);

            Example2 ex2 = new Example2();
            ex2.Process(myLogger); 
            // In our case, the function Logger with a parameter was being called by Process function in the Example2 class. 
            // This approach is called Callback.
        }
    }
}
